/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pressa.client.service;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author Lucas Borri (lborri@pressa.com.ar)
 */
public class UDPService {
    private int portOut = 4004;
    public boolean sendPacket(String packet, String ipPort) {
        try {
            String ip = ipPort.substring(0, ipPort.indexOf(':'));
            String port = ipPort.substring((ipPort.indexOf(':') + 1), ipPort.length());
            int portInt = Integer.parseInt(port);
            DatagramSocket socketOut = new DatagramSocket(getPortOut());                //Obtenemos un socket
            byte[] packetBuffer = packet.getBytes();                            //Convertimos el packete a enviar en cadena de bytes
            InetAddress sendIP = InetAddress.getByName(ip);
            DatagramPacket packetOut = new DatagramPacket(packetBuffer, packetBuffer.length, sendIP, portInt); //Creamos el objeto DatagramPacket que se va a enviar
            socketOut.send(packetOut);                                          //Enviamos el paquete
            socketOut.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    
    public String receivePacket(String port) {
        String packet = "";                                                     //String a retornar
        byte[] packetInBuffer = new byte[192];                                  //Buffer para guardar la respuesta
        int portInt = Integer.parseInt(port);
        DatagramSocket socketIn = null;
        try {
            socketIn = new DatagramSocket(portInt);                             //Creamos el socket
            DatagramPacket packetIn = new DatagramPacket(packetInBuffer, packetInBuffer.length);
            socketIn.setSoTimeout(1000);                                        //Configuramos el timeout
            socketIn.receive(packetIn);                                         //Recibimos el packet
            //System.out.println("Packet recibido!");
            byte[] response = packetIn.getData();                               //Lo guardamos en un array de bytes
            packet = new String(response);                                      //Transformo el array de bytes en un String
        } catch (Exception e) {
            //System.out.println("Port TimeOut!(" + portInt + ") ");
        } finally {
            socketIn.close();
        }
        return packet.trim();                                                   //Elimino los espacios
    }
    
     public String receiveAckPacket(String port) {
        String packet = "";                                                     //String a retornar
        byte[] packetInBuffer = new byte[128];                                  //Buffer para guardar la respuesta
        int portInt = Integer.parseInt(port);
        DatagramSocket socketIn = null;
        try {
            socketIn = new DatagramSocket(portInt);                             //Creamos el socket
            DatagramPacket packetIn = new DatagramPacket(packetInBuffer, packetInBuffer.length);
            socketIn.setSoTimeout(5000);                                        //Configuramos el timeout
            socketIn.receive(packetIn);                                         //Recibimos el packet
            byte[] response = packetIn.getData();                               //Lo guardamos en un array de bytes
            packet = new String(response);                                      //Transformo el array de bytes en un String
        } catch (Exception e) {
            System.out.println("Port TimeOut!(" + portInt + ")(ACK) ");
        } finally {
            socketIn.close();
        }
        return packet.trim();                                                   //Elimino los espacios
    }
     
    public void sendAckPacket(){
        
      
        
        
    }

    public int getPortOut() {
        return portOut;
    }

       public void setPortOut(int portOut) {
        this.portOut = portOut;
    }
}